<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/js/validate.js"></script>
<title></title>
<script type="text/javascript">
	Ext.onReady(function(){
		Ext.QuickTips.init();
		var resultWidth = Ext.get("process-grid").getWidth();
		var userId = '${__SATURN_AUTORITY_USER__.id}';
		var states = [['10','运行'], ['12','完成']];
        //用于下拉列表的store
        var simpleStore = new Ext.data.SimpleStore({
            fields: ['state','stateName']
        });
        simpleStore.loadData(states);
        
        //用于下拉列表的store
        var timeoutData = [['Y','是'], ['N','否']];
        var timeoutStore = new Ext.data.SimpleStore({
            fields: ['fieldKey','fieldValue']
        });
        timeoutStore.loadData(timeoutData);

		var simple = new Ext.FormPanel({
			renderTo:'form-ct',
	        frame:true,
	        title: '查询条件',
	        bodyStyle:'width:100%',
	        labelWidth:resultWidth*0.12,
	       items: [{
	            layout:'column',
	            items:[{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    allowDecimals:false,
		                    fieldLabel: '工作项ID',
		                    name: 'id',
		                    id: 'id',
		                    maxLength:10,
		                    vtype:'isNumber',
		                    msgTarget:'qtip'
		                },{
		                    xtype:'combo',
		                    fieldLabel: '是否超时',
		                    name:'is_timeout',
		                    hiddenName:'isTimeout',
		                    id: 'is_timeout',
		                    store: timeoutStore,
		                    displayField: 'fieldValue',
		                    valueField: 'fieldKey',
		                    mode: 'local',
						    triggerAction: 'all',
						    width:130,
						    editable: false,
 							typeAhead: true
	                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '工作项名称',
		                    name: 'workitem_name',
		                    id: 'workitem_name',
		                    maxLength:10,
		                    vtype:'isNumberAlphaUnderlineChinese',
		                    msgTarget:'qtip'
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '流程实例名称',
		                    name: 'instance_name',
		                    id: 'instance_name',
		                    maxLength:10,
		                    vtype:'isNumberAlphaUnderlineChinese',
		                    msgTarget:'qtip'
		                }]
	            },{
	            	columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'datefield',
		                    fieldLabel: '工作项开始时间',
		                    name: 'start_time_from',
		                    id: 'start_time_from'
		                }]
	            },{
	                layout: 'column',
	               	columnWidth:.25,
	                items: [{
		                    xtype:'label',
		                    text: '--'
		                },{
		                    xtype:'datefield',
		                    name: 'start_time_to',
		                    id: 'start_time_to'
		                }]
	            },{
	            	columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'datefield',
		                    fieldLabel: '工作项结束时间',
		                    name: 'end_time_from',
		                    id: 'end_time_from'
		                }]
	            },{
	                layout: 'column',
	                columnWidth:.25,
	                items: [{
		                    xtype:'label',
		                    text: '--'
		                },{
		                    xtype:'datefield',
		                    name: 'end_time_to',
		                    id: 'end_time_to'
		                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'combo',
		                    fieldLabel: '当前状态',
		                    name:'workitem_state',
		                    hiddenName:'workitem_state',
		                    id: 'current_state',
		                    store: simpleStore,
		                    displayField: 'stateName',
		                    valueField: 'state',
		                    mode: 'local',
						    triggerAction: 'all',
						    width:130,
						    editable: false,
 							typeAhead: true
		                }]
		          }]
		      }],

	        buttons: [{
	            text: '确定',
	            handler:function(){
	            if(simple.form.isValid()){
	            var start_time_from = Ext.get("start_time_from").dom.value;
	            var start_time_to = Ext.get("start_time_to").dom.value;
	            var end_time_from = Ext.get("end_time_from").dom.value;
	            var end_time_to = Ext.get("end_time_to").dom.value;
	            if(validate(start_time_from,start_time_to,end_time_from,end_time_to)){
		            if(start_time_from!=''){
		            	start_time_from = start_time_from+" 00:00:00";
		            }
		             if(start_time_to!=''){
		             	start_time_to = start_time_to+" 23:59:59";
		            }
	    	        if(end_time_from!=''){
	            		end_time_from = end_time_from+" 00:00:00";
	            	}
		            if(end_time_to!=''){
		            	end_time_to = end_time_to+" 23:59:59";
			        }
					store.load({params: {start:0,
								limit:20,
								workitem_name:Ext.get("workitem_name").dom.value,
								process_instance_name:Ext.get("instance_name").dom.value,
								workitem_id:Ext.get("id").dom.value,
								start_time_from: start_time_from,
								start_time_to:start_time_to,
								end_time_from:end_time_from,
								end_time_to:end_time_to,
								is_timeout:Ext.get("isTimeout").dom.value,
								state:Ext.get("workitem_state").getValue()}});
	            }
	            }
            }
	        },{
	            text: '重置',
	            handler:function(){
	            	simple.getForm().reset();
	            }
	        }]
	    });
		
		var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/queryModule/action/QueryWorkitemList.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
	               {name: 'workitem_id', mapping: 'workitem_id'},
	               {name: 'workitem_name', mapping: 'name'},
	               {name: 'workitem_state', mapping: 'state'},
	               {name: 'process_instance_name', mapping: 'process_instance_name'},
	               {name: 'workitem_is_timeout', mapping: 'is_timeout'}, 
		    	   {name: 'workitem_start_time', mapping: 'start_time'},
		    	   {name: 'workitem_end_time', mapping: 'end_time'}
	           ]
		});
		
	  
	    var grid = new Ext.grid.GridPanel({
       	    store: store,
	        columns: [	 
	            {header: "工作项ID", width:resultWidth*0.1, dataIndex: 'workitem_id', sortable: true},
	            {header: "工作项名称", width:resultWidth*0.15, dataIndex: 'workitem_name', sortable: true,renderer:showOp},
	            {header: "当前状态", width: resultWidth*0.1, dataIndex: 'workitem_state', sortable: true,renderer:showState},
	            {header: "流程实例名称", width:resultWidth*0.15, dataIndex: 'process_instance_name', sortable: true},
	            {header: "是否超时", width:resultWidth*0.1, dataIndex: 'workitem_is_timeout', sortable: true,renderer:timeout},
	            {header: "工作项开始时间", width:resultWidth*0.15, dataIndex: 'workitem_start_time', sortable: true},
	            {header: "工作项结束时间", width:resultWidth*0.15, dataIndex: 'workitem_end_time', sortable: true}
	        ],
	        renderTo:'process-grid',
	        height:600,
	        frame:true,
	        loadMask: {msg:'正在加载数据，请稍侯……'}, 
	        bodyStyle:'width:100%',
	        bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
		    }) 
    });
   	store.load({params: {start:0,limit:20}});
   		store.on('beforeload',function(){
  		if(simple.form.isValid()){
	            var start_time_from = Ext.get("start_time_from").dom.value;
	            var start_time_to = Ext.get("start_time_to").dom.value;
	            var end_time_from = Ext.get("end_time_from").dom.value;
	            var end_time_to = Ext.get("end_time_to").dom.value;
	            if(validate(start_time_from,start_time_to,end_time_from,end_time_to)){
	               if(start_time_from!=''){
		            	start_time_from = start_time_from+" 00:00:00";
		            }
		             if(start_time_to!=''){
		             	start_time_to = start_time_to+" 23:59:59";
		            }
	    	        if(end_time_from!=''){
	            		end_time_from = end_time_from+" 00:00:00";
	            	}
		            if(end_time_to!=''){
		            	end_time_to = end_time_to+" 23:59:59";
			        }
		            Ext.apply(this.baseParams,
		            		   {workitem_name:Ext.get("workitem_name").dom.value,
								process_instance_name:Ext.get("instance_name").dom.value,
								workitem_id:Ext.get("id").dom.value,
								start_time_from: start_time_from,
								start_time_to:start_time_to,
								end_time_from:end_time_from,
								end_time_to:end_time_to,
								state:Ext.get("workitem_state").getValue()});
		            }else{
		            	return false;
		            }
		  }else{
		  		return false;
		  }
    });
   	function showState(value){
		if(value=='10')
			return "运行";
		if(value=='12')
			return "完成";
	}
	
	function timeout(value){
		if(value=='Y'){
			return "是";
		}else{
			return "否";
		}
	}
	
   	function showOp(value,cellmeta,record){	
			return "<a href='javascript:showWorkitemInfo("+record.data["workitem_id"]+")'>"+value+"</a>";	
	}	
	
	function validate(start_from,start_to,end_from,end_to){
		if(start_from!=''&&start_to!=''){
			var start_time_from = new Date(start_from);
			var start_time_to = new Date(start_to);
			if(start_time_from>start_time_to){
				Ext.MessageBox.alert("工作项开始时间范围选取不正确");
				return false;
			}
		}
		if(end_from!=''&&end_to!=''){
			var end_time_from = new Date(end_from);
			var end_time_to = new Date(end_to);
			if(end_time_from>end_time_to){
				Ext.MessageBox.alert("工作项结束时间范围选取不正确");
				return false;
			}
		}
		if(start_from!=''&&end_to!=''){
			var end_time_to = new Date(end_to);
			var start_time_from = new Date(start_from);
			if(start_time_from>end_time_to){
				Ext.MessageBox.alert("流程开始时间不能大于流程的结束时间");
				return false;
			}
		}
		return true;
	}
});

function showWorkitemInfo(id){
	  var workitemStore = new Ext.data.JsonStore({
		    url: "${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/queryModule/action/QueryWorkitemInfo.action",
		    root: 'result',
		    fields: [
	               {name: 'workitem_id', mapping: 'workitem_id'},
	               {name: 'name', mapping: 'name'},
		    	   {name: 'process_define_name', mapping: 'process_define_name'},
	               {name: 'state', mapping: 'state'},
	               {name: 'priority', mapping: 'priority'},
	               {name: 'is_timeout', mapping: 'is_timeout'},
	               {name: 'limit_number', mapping: 'limit_number'},
	               {name: 'limit_number_description', mapping: 'limit_number_description'},
	               {name: 'start_time', mapping: 'start_time'},
	               {name: 'participant', mapping: 'assistant_name'},
	               {name: 'end_time', mapping: 'end_time'},
	               {name: 'activity_instance_name', mapping: 'activity_instance_name'},
	               {name: 'process_instance_name', mapping: 'process_instance_name'}
	           ]
		});
	   	workitemStore.load({params: {workitem_id:id}});
	    
	    workitemStore.on('load',function(){
	    var	record = workitemStore.getAt(0);
	    var panel = new Ext.FormPanel({
	        frame:true,
	        bodyStyle:'padding:5px 5px 0',
	       items: [{
	            layout:'column',
	            items:[{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '工作项ID',
		                    name: 'workitem_id',
		                    id: 'workitem_id',
		                    value:record.data["workitem_id"],
		                     disabled:true
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '工作项名称',
		                    name: 'name',
		                    id: 'name',
		                    value:record.data["name"],
		                     disabled:true
		                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '流程定义名称',
		                    name: 'process_define_name',
		                    value:record.data["process_define_name"],
		                    id: 'process_define_name',
		                     disabled:true
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '当前状态',
		                    name: 'state',
		                    id: 'state',
		                    value:record.data["state"]=='12'?"完成":"运行",
		                    disabled:true
		                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '优先级',
		                    name: 'priority',
		                    id: 'priority',
		                    value:record.data["priority"],
		                    disabled:true
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '是否超时',
		                    name: 'workitem_is_timeout',
		                    id: 'workitem_is_timeout',
		                    value:showIsTimeout(record.data["is_timeout"]),
		                    disabled:true
		                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '限制时间',
		                    name: 'limit_number',
		                    id: 'limit_number',
		                     value:record.data["limit_number"],
		                      disabled:true
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '限制时间描述',
		                    name: 'limit_number_description',
		                    id: 'limit_number_description',
		                     value:record.data["limit_number_description"],
		                      disabled:true
		                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '流程启动时间',
		                    name: 'start_time',
		                    id: 'start_time',
		                    value:record.data["start_time"],
		                    disabled:true
		                },{
		                    xtype:'textfield',
		                    fieldLabel: '流程实例名称',
		                    name: 'process_instance_name',
		                    id: 'process_instance_name',
		                    value:record.data["process_instance_name"],
		                     disabled:true
		                }]
	            },{
	                columnWidth:.5,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '流程结束时间',
		                    name: 'end_time',
		                    id: 'end_time',
		                    value:record.data["end_time"],
		                      disabled:true
		                }, {
		                    xtype:'textfield',
		                    fieldLabel: '活动实例名称',
		                    name: 'activity_instance_name',
		                    id: 'activity_instance_name',
		                    value:record.data["activity_instance_name"],
		                    disabled:true
		                }]
	            }] 
        }], buttons: [{
	            text: '返回',
	            handler:function(){
	            	window.close();
	            }
       		 }]
	    });
			
		    var window = new Ext.Window({
				title: '工作项实例信息',
				width: 700,
				height:300,
				minWidth: 300,
				minHeight: 200,
				layout: 'fit',
				plain:true,
				items:panel,
				buttonAlign:'center',
				modal:true
			});
			window.show();
	    })
	
		
	}
	
	function showIsTimeout(is_timeout){
		if(is_timeout=='Y'){
			return "是";
		}else{
			return "否";
		}
	}
</script>
<div id='form-ct' width='100%'></div>
<div id='process-grid' width='100%'></div>
</head>
</html>